<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%String path = request.getContextPath() ;%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %> 
<%@ taglib uri="/WEB-INF/tld/ui.tld" prefix="ui" %> 
<!DOCTYPE html>
<html lang="zh-CN" >
<head>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>金诚业务系统登录用户管理</title>
<jsp:include page="../../import.jsp" flush="true" />
</head>


<body class="iframeBody">
<div class="Wrapper">
<div class="searchArea">
    <form id="searchBar">
        <div class="fieldset1 clearFix">
             <div class="fieldBox">
                <div class="label">手机号</div>
                <div class="field">
                    <input name="usrNm" id="usrNm" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">真实名称</div>
                <div class="field">
                    <input name="rlNm" id="rlNm" type="text" class="ui-textbox" value="">
                </div>
            </div>
            <div class="fieldBox">
                <div class="label">状态</div>
                <div class="field">
                    <select name="isValid" id="isValid">
                        <option value=" " seleted>请选择</option>
                        <c:forEach items="${isValidList }" var="isValid">
                        	<option value="${isValid.key }">${isValid.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">所属系统</div>
                <div class="field">
                    <select name="searchSysCd" id="searhcSysCdId">
                        <c:forEach items="${sysCdCdList }" var="sysCdTemp">
                        	<option value="${sysCdTemp.key }">${sysCdTemp.value }</option>
                        </c:forEach>
                    </select>
                </div>
            </div>

            <div class="clearFix"></div>
            <div class="btnsBox">
                <span id="searchBtn" class="sq-button sq-button-round">查询</span>
                <span id="addBtn" class="sq-button sq-button-round">新增</span>
                <span id="changeBtn" class="sq-button sq-button-round">修改</span>
                <span id="reSetPwdBtn" class="sq-button sq-button-round">密码重置</span>
            </div>
        </div>
    </form>
</div>
<div id="ligergrid1" class="sq-grid"></div>
</div>
<div id="addUsr_Html" style="display:none">
    <form id="addUsr_form">
    	 <input name="idS" id="idS" type="text" style="display: none;" value=""/>
        <div class="fieldset1 tabel_col tabel_col_s noBorder clearFix">
            <div class="fieldBox col_50">
                <div class="label">手机号</div>
                <div class="field">
                    <input name="usrNm" id="usrNmAdd" type="text" class="ui-textbox" value="" validate="{required:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">真实名称</div>
                <div class="field">
                    <input name="rlNm" id="rlNmAdd" type="text" class="ui-textbox" value="" validate="{required:true}">
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">所属系统</div>
                <div class="field">
                    <select name="sysCd" id="sysCdId"  validate="{required:true, select: true}">
                        <c:forEach items="${sysCdCdList }" var="sysCdTemp">
                        	<option value="${sysCdTemp.key }">${sysCdTemp.value }</option>
                        </c:forEach>
                    </select>
                    <span class="requiredSymbol">*</span>
                </div>
            </div>
            
            <div class="fieldBox col_50">
                <div class="label">状态</div>
                <div class="field">
                    <select name="refIsValid" id="refIsValid" validate="{required:true, select: true}">
                        <option value="" seleted>请选择</option>
                        <c:forEach items="${isValidList }" var="isValid">
                        	<option value="${isValid.key }">${isValid.value }</option>
                        </c:forEach>
                    </select>
                    <span class="requiredSymbol">*</span>
                </div>
            </div>

            	<div class="fieldBox col_50">
                    <div class="label">员工职位</div>
                    <div class="field">
                        <select name="employeePosCd" id="employeePosCdId" data-value="" validate="{required:true, select: true}">
			               <option value="" seleted>请选择</option>
			               <c:forEach items="${employeePosCdList }" var="kv" begin="0" step="1" varStatus="status">
								<option value="${kv.key }">${kv.value }</option>
							</c:forEach>
			            </select>
			            <span class="requiredSymbol">*</span>
                    </div>
                </div>
        </div>
        <div class="btnsBox">
            <span id="saveNewUsr_Btn" class="sq-button sq-button-hasicon Hide"><i class="sqBtnIco_10"></i>保存</span>
            <span id="changeSave_Btn" class="sq-button sq-button-hasicon Hide"><i class="sqBtnIco_10"></i>修改</span>
            <span class="sq-button sq-button-hasicon l-dialog-btn-cancel Hide"><i class="sqBtnIco_14"></i>取消</span>
            <input id="resetBtn" type="reset" value="重置" class="l-button l-button-reset Hide"/>
        </div>
    </form>
</div>
<div id="infoUsr_Html" style="display:none">
    <form id="infoUsr_form">
        <div class="fieldset1 tabel_col tabel_col_s noBorder clearFix">
            <div class="fieldBox col_50">
                <div class="label">手机号</div>
                <div class="field">
                    <span id="usrNmInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">真实名称</div>
                <div class="field">
                	<span id="refRlNmInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">注册时间</div>
                <div class="field">
                    <span id="refRegDtInfo"></span>
                </div>
            </div>
            <div class="fieldBox col_50">
                <div class="label">状态</div>
                <div class="field">
                    <span id="refIsValidInfo"></span>
                </div>
            </div>
        </div>
    </form>
</div>

<div id="infoSun_Html" style="display:none">
    <div id="ligergrid2" class="sq-grid"></div>
</div>

<script type="text/javascript">

var grid = null,
	Editable=true;
var dialogForAddMain;
$(function () {
	//=========有供应商存在时禁止操作字段E==========//
	grid =  $("#ligergrid1").ligerGrid({
    	url: '<%=path%>/system/sysUsr/initJCSplUsrList.do?splId='+${splId},
    	delayLoad:true,
    	method : 'get',
        columns: [
            { display: '手机号', name: 'usrNm', minWidth: 140,
            	render: function (rowdata, rowindex, value, column) { 
        		var h = '';
    			if(value&&value!="null"){
    		    h+='<a href="javascript:detailBtn(';
                h+="'"+rowdata.id+"','"+rowdata.refSysCd+"'";
                h+=')">'+value+'</a>'
    			}
                return h;
    		},enabledSort:true},
            { display: '真实名称', name: 'refRlNm', minWidth: 140, enabledSort:true },
            { display: '员工职位', name: 'employeePosCdNm', minWidth: 140, enabledSort:false },
            { display: '所属供应商', name: 'splNm', minWidth: 140, enabledSort:false },
            { display: '数据来源', name: 'regChlCd', minWidth: 140, enabledSort:false,
            	render: function (rowdata, rowindex, value, column) { 
            		var h = 'PC后台';
                    return h;
        		} 
            },
            { display: '账号状态', name: 'refIsValidNm', minWidth: 100 , enabledSort:true} ,
            { display: '创建时间', name: 'crtTm', width: 180 , render:renderDate, enabledSort:true},
            { display: '创建人', name: 'crtUsrNm', width: 180 , enabledSort:true},
            { display: '操作',
                isSort: false,
                width: 180,
                align:'left',
                render: function (rowdata, rowindex, value){
                	 var h="";
                	if(StringUtils.isNotEmpty(rowdata.id)){
                		h+='<a class="sq-button-hasicon" href="javascript:removeSplUsrRlInfo(';
                        h+="'"+rowdata.id+"',";
                        h+="'"+rowdata.employeePosCd+"'";
                        h+=')"><i class="sqBtnIco_7"></i>删除</a>';
                	}
                    return h;
                }
            }
        ],
        pageSize:10,
        root:'rows',
        record:'total',
        hideLoadButton : true,
        width: '100%',
        isScroll: false,
        checkbox:true,
        rownumbers:true,
        onRenderComplete: function () {
        	iframeResize ();
        },
        onSelectRow: function (rowdata, rowid, rowobj) {
        	isEditable(rowdata.refIsSysDef);
        },
        onSuccess: function (data, grid) {
        }
    });

    $("#searchBar").ligerForm({
    });

    $("#pageloading").hide();
    $("#searchBtn").click(function(event) {
    	reload();
    });
    
    //刷新列表
    function reload(){
    	var form = liger.get("searchBar");
        var data = form.getData();
        for (name in data){
           grid.setParm(name, $.trim(data[name]));
        };
       grid.reload();
    }
	
    $("#addBtn").click(function(event) {
    	if(liger.get("addUsr_form")){
    		var form=liger.get("addUsr_form");
    		$("#resetBtn").click();  //重置 input-text
            	form.setData({  //重置表单
            });
    	}
    	dialogForAddMain = $.ligerDialog.SQopen({
            width: 760,
            // top: 100,
            buttons: [
                {
                    text: "保存", cls: 'l-dialog-btn-ok', onclick:function(){
                    	$("#saveNewUsr_Btn").click();
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
            title : '新增用户',
            target: $("#addUsr_Html")
        });
        var ligerForm = $("#addUsr_form").ligerForm({validate: true});
        liger.get("sysCdId").setEnabled();
        liger.get("usrNmAdd").setEnabled();
        liger.get("rlNmAdd").setEnabled();
    });
    $("#saveNewUsr_Btn").click(function(event) {
        var form = liger.get("addUsr_form");
        if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        addUsrMethod(form);
    });
    
    $("#changeBtn").click(function(event) {
    	var selectedRows = grid.getSelectedRows();
		if(selectedRows.length != 1){
			top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW,SQ_type: "warning"});
			return;
		}
		
		if(Editable==false){
    		top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_INSIDE_FORBID_HANDLE,SQ_type: "warning"});
    		return;
    	}; 
		
		var _id = selectedRows[0].id;
		var _sysCd = selectedRows[0].refSysCd;
		dialogForAddMain = $.ligerDialog.SQopen({
            width: 760,
            // top: 100,
             buttons: [
                {
                    text: "保存", cls: 'l-dialog-btn-ok', onclick:function(){
                    	$("#changeSave_Btn").click();
                    }
                }, {
                    text: "取消", cls: 'l-dialog-btn-no', onclick:function(){
                    	$.ligerDialog.hide();
                    }
                }
            ],
            title : '修改用户',
            target: $("#addUsr_Html")
        });
        //初始化数据
        initData(_id, _sysCd);
        //禁止编辑
        var ligerForm = $("#addUsr_form").ligerForm({validate: true});
        liger.get("sysCdId").setDisabled();
        liger.get("usrNmAdd").setDisabled();
        liger.get("rlNmAdd").setDisabled();
    });
    
    //修改
    $("#changeSave_Btn").click(function(event) {
        var form = liger.get("addUsr_form");
        if (!form.valid()) {
            top.$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO,SQ_type: "warning"});
            return false; //验证不通过，不执行以下代码
        }
        var data = form.getData();
        data["splId"] = ${splId };
        var waitingDialog=null;

         $.ajax({
             url: '<%=path%>/system/sysUsr/modifyJCSplUsr.do',
             type: 'post',
             dataType: 'json',
             data: data,
            beforeSend: function (msg) {
                 manager = $.ligerDialog.waitting(LogUtils.LOG_CMM_DATA_LOADING);
            },
             success: function (msg) {
                manager.close();
                if(msg.success == 1){
                	$.ligerDialog.hide();
                	top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
					 grid.reload();
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
             }
         });
    });
    
    //密码重置
	$("#reSetPwdBtn").click(function(event) {
		var selectedRows = grid.getSelectedRows();
		if(selectedRows.length == 0){
			top.$.ligerDialog.SQalert({content: LogUtils.LOG_SYS_ORG_SELECT_RESET_DATA,SQ_type: "warning"});
			return;
		}
		
		var _ids = [];
		
		for ( var index in selectedRows) {
			_ids.push(selectedRows[index].id);
		}
		$.ligerDialog.confirm("确认重置密码为【ACd.123456】", function (flag) {
			if(flag == false)return;
			$.ajax({
				type: 'post',
				dataType:'json',
				data:{ids:JSON.stringify(_ids)},
				url: '<%=path%>/system/sysUsr/reSetJCPwd.do',
				success:function(msg){
					 if(msg.success == 1){
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
						 grid.reload();
					 }else{
						 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
					 }
				}
			});
		});
	});
	//加载数据
	reload();

});

	//删除供应商和用户关系
	function removeSplUsrRlInfo(_usrId,_employeePosCd){
		$.ligerDialog.confirm("确定删除吗?", function (flag) {
			if(flag == false)return;
			var data = {
					usrId:_usrId,
                    employeePosCd:_employeePosCd,
					splId:${splId }
			}
			var _url = '<%=path%>/system/sysUsr/removeJCSplUsrRlInfo.do';
			AjaxUtils.ajaxPost(_url, data, function(msg){
				if(msg.success == 1){
					 UIUtils.alertSuccess(msg.info);
					 grid.reload();
				 }else{
					 UIUtils.alertError(msg.info);
				 }
			},true);
		});
	}
	
	function detailBtn(_id,_sysCd) {
		$("#changeSave_Btn").hide();
		$("#saveNewUsr_Btn").hide();
	    $.ligerDialog.open({
	        width: 760,
	        // top: 100,
	        buttons: [
	                {
	                    text: "关闭", cls: 'l-dialog-btn-no', onclick:function(){
	                    	$.ligerDialog.hide();
	                    }
	                }
	            ],
	        title : '用户详情',
	        target: $("#infoUsr_Html")
	    });
	  //初始化数据
	    initInfoData(_id, _sysCd);
	    var ligerForm = $("#infoUsr_form").ligerForm({validate: true});
	}

	/*
	是否系统内置参数获取
	*/
	function isEditable(isSysDef){
		if(isSysDef==1){
			Editable=false;
		}else{
			Editable=true;
		}
	}
	function iframeResize_pageIn () {
	    if (parent.pageInApp) {
	        parent.pageInApp.pageInTab.onResize();
	        iframeResize ()
	    };
	}
	function iframeResize () {
	    if (parent.parent.mainApp) {
	        parent.parent.mainApp.parentTab.onResize();
	    };
	}

	function initData(_id, _sysCd){
		if(!_id)return;
		var splId = ${splId };
		var _url = '<%=path%>/system/sysUsr/getJCSplUsrById.do?id='+_id;
		$.ajax({
			type: 'get',
			dataType:'json',
			data:{id:_id, sysCd:_sysCd, splId:splId},
			url: _url,
			success:function(msg){
				 if(msg.success == 1){
					 setData(msg.data);//设置数据
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
		});
	}
	function initInfoData(_id, _sysCd){
		if(!_id)return;
		var splId = ${splId };
		var _url = '<%=path%>/system/sysUsr/getJCSplUsrById.do?';
		$.ajax({
			type: 'get',
			dataType:'json',
			data:{id:_id, sysCd:_sysCd, splId:splId},
			url: _url,
			success:function(msg){
				 if(msg.success == 1){
					 setInfoData(msg.data);//设置数据
				 }else{
					 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
				 }
			}
		});
	}
	//设置页面数据
	function setData(data){
		
		var form = new liger.get("addUsr_form");
		form.setData({
			usrTypCd:data.usrTypCd,
			sysCd:data.refSysCd,
			refIsValid:data.refIsValid,
			employeePosCd:data.employeePosCd
		});
		$("#idS").val(data.id);
		$("#usrNmAdd").val(data.usrNm);
		$("#rlNmAdd").val(data.refRlNm);
	}
	//设置页面数据
	function setInfoData(data){
		var form = new liger.get("infoUsr_form");
		$("#usrNmInfo").text(isNulltoNBSP(data.usrNm));
		$("#refRlNmInfo").text(isNulltoNBSP(data.refRlNm));
		$("#refRegTypCdInfo").text(isNulltoNBSP(data.refRegTypCdNm));
		$("#refRegDtInfo").text(isNulltoNBSP(DateUtils.formatDate(new Date(data.refRegDt), DateUtils.YYYY_MM_DDHHMMSS)));
		$("#refIsValidInfo").text(isNulltoNBSP(data.refIsValidNm));
		
	}
	function isNulltoNBSP(a){
		if(a==null || a==undefined){
			return ""
		}else{
			return a;
		}
	}
	
	function addUsrMethod(form){
		var data = form.getData();
	    var genderCd = $('input[name="genderCd"]:checked ').val();
	    var _splId = ${splId };
	    var _splNm = '${splNm }';
	    data["genderCd"] = genderCd;
	    data["splId"] = _splId;
	    data["splNm"] = _splNm;
	    var waitingDialog=null;
	    var _url = '<%=path%>/system/sysUsr/addJCSplUsr.do';
	    AjaxUtils.ajaxPost(_url, data, function(msg){
			if(msg.success == 1){
				 $.ligerDialog.hide();
				 UIUtils.alertSuccess(msg.info);
				 grid.reload();
			 }else{
				 UIUtils.alertError(msg.info);
			 }
		},true);
	}
	
	//格式化时间
	function renderDate(rowdata, index, value){
		var dateStr = "";
		if(null != value){
			var date = new Date(value);
			dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
		}
		return dateStr;
	}
</script>
</body>
</html>